﻿package org.hive.managers.plugin
{
	import caurina.transitions.*;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.utils.getQualifiedClassName;
	
	import org.hive.Hive;
	import org.hive.events.*;
	import org.hive.managers.*;
	import org.hive.managers.utils.LayoutArrange;
	import org.hive.sequence.Sequence;
	public class LayoutManager extends Manager
	{
        //________________________________________________ PROPERTIES
       
		private static var __stage : DisplayObjectContainer;
		private static var __stageWidth : Number = 0;
		private static var __stageHeight : Number = 0;
		

		//________________________________________________ CONSTRUCTOR
		
		public function LayoutManager(hive:Hive):void {
			super(hive);
		}
		
		public static function get key():String {
			return getQualifiedClassName(LayoutManager);
		}
		
		public static function init(hive:Hive):void
		{
			if(Hive.DEBUG_MODE){trace( "LayoutManager :: init" )}

			var manager:LayoutManager = new LayoutManager(hive);
			hive.registerManager(getQualifiedClassName(LayoutManager), manager);

			
			//_____________ Register Object :: DisplayObject
			function displayObjectFunc( action:*, sequence:Sequence, positionInSequence:int ):void {
				if( sequence.getParams( action ) != null){
					manager.addChildAt( action, sequence.getParams( action )[0] );
				} else manager.addChild( action );
			}
			hive.sequenceManager.registerAction(DisplayObject, displayObjectFunc);


			__stage = hive.canvas;
			
			__stageWidth = __stage.width;
			__stageHeight = __stage.height;
		}
		

		

		
       
		//________________________________________________ DISPLAY LIST METHODS

        /**
        *
        * @param	element
        * @param	index
        * @return
        */
		private function buildElement( element:*, index:Number=0 ):DisplayObject
		{
			if( index ){
				return __stage.addChildAt( element, index );
			}else{
				return __stage.addChild( element );
			}
		}
		
        /**
        *
        * @param	arrangement
        * @param	target
        * @param	relativeTo
        */
		public function arrange( arrangement:String, target:DisplayObject, relativeTo:DisplayObject=null ):*
		{
			if( !__stage.contains( target ) ) return null;
			
			var index:Number = -1;
			switch( arrangement ){
				
				case LayoutArrange.BRING_TO_BACK:
					index = 0
				break;
				
				case LayoutArrange.BRING_TO_FRONT:
					index = __stage.numChildren - 1;
				break;
				
				case LayoutArrange.BRING_BACKWARD:
					index = __stage.getChildIndex( target ) - 1;
				break;
				
				case LayoutArrange.BRING_FORWARD:
					index = __stage.getChildIndex( target ) + 1;
				break;
				
				case LayoutArrange.BEHIND:
					index = __stage.getChildIndex( relativeTo ) - 1;
				break;
				
				case LayoutArrange.INFRONT:
					index = __stage.getChildIndex( relativeTo ) + 1;
				break;
			}
			
			if( index != -1 )	__stage.setChildIndex( target, index );
			
			return index;
		}

        /**
        *
        * @param	child
        * @return
        */
		public function getChildIndex( child:DisplayObject ):Number
		{
			return Number(__stage.getChildIndex( child ));
		}

        /**
        *
        * @param	child
        * @return
        */
		public function addChild( child:DisplayObject ):DisplayObject
		{
			return buildElement( child );
		}
		
        /**
        *
        * @param	child
        * @param	index
        * @return
        */
		public function addChildAt( child:DisplayObject, index:Number=0 ):DisplayObject
		{
			return buildElement( child, index );
		}
		
        /**
        *
        * @param	...children
        * @return
        */
		public function removeChildren( ...children ):void
		{
			for each( var child:DisplayObject in children ) removeChild( child );
		}
		
        /**
        *
        * @param	child
        * @return
        */
		public function removeChild( child:DisplayObject ):DisplayObject
		{
			return __stage.removeChild( child );
		}
		
        /**
        *
        * @param	index
        * @return
        */
		public function removeChildAt( index:Number ):DisplayObject
		{
			return __stage.removeChildAt( index );
		}
		
		

        /**
        *
        * @param	name
        * @return
        */
		public function getChildByName( name:String ):DisplayObject
		{
			return __stage.getChildByName( name );
		}
		
        /**
        *
        * @param	index
        * @return
        */
		public function getChildAt( index:Number ):DisplayObject
		{
			return __stage.getChildAt( index );
		}
		
        /**
        *
        * @return
        */
		public function get stageWidth( ):Number
		{
			return __stage.stage.stageWidth;
		}
		
        /**
        *
        * @return
        */
		public function get stageHeight( ):Number
		{
			return __stage.stage.stageHeight;
		}
		
        /**
        *
        * @return
        */
		public function get stageCenterX( ):Number
		{
			return __stage.stage.stageWidth/2;
		}
		
        /**
        *
        * @return
        */
		public function get stageCenterY( ):Number
		{
			return __stage.stage.stageHeight/2;
		}
		
        /**
        *
        * @return
        */
		public function get stage( ):Stage
		{
			return __stage.stage;
		}
		
        /**
        *
        * @return
        */
		public function get documentClass( ):DisplayObjectContainer
		{
			return __stage;
		}
		
        /**
        *
        * @param	child
        * @return
        */
		public function contains( child:DisplayObject ):Boolean
		{
			return __stage.contains(child);
		}

        /**
        *
        * @return
        */
        public function get stageLeft( ):Number
        {
            return __stageWidth - stageWidth / 2;
        }

        /**
        *
        * @return
        */
        public function get stageRight( ):Number
        {
            return stageLeft + stageWidth;
        }

        /**
        *
        * @return
        */
        public function get stageTop( ):Number
        {
            return __stageHeight - stageHeight / 2;
        }

        /**
        *
        * @return
        */
        public function get stageBottom( ):Number
        {
            return stageTop + stageHeight;
        }
		
		
		/**
        *
        */
		public static var blocked		: Boolean = false;
		
		/**
        *
        * 
        */
		public function blockInteraction():void
		{
			blocked = true;
			hive.canvas.addEventListener( MouseEvent.CLICK, handleBlockInteraction, true, 0, true );
			hive.canvas.addEventListener( MouseEvent.MOUSE_DOWN, handleBlockInteraction, true, 0, true );
			hive.canvas.addEventListener( TextEvent.LINK, handleBlockInteraction, true, 0, true );
		}
		
		/**
        *
        * 
        */
		public function unblockInteraction():void
		{
			blocked = false;
			hive.canvas.removeEventListener( MouseEvent.CLICK, handleBlockInteraction, true );
			hive.canvas.removeEventListener( MouseEvent.MOUSE_DOWN, handleBlockInteraction, true );
			hive.canvas.removeEventListener( TextEvent.LINK, handleBlockInteraction, true );
		}
		
		/**
        *
        * @param	e
        */
		private function handleBlockInteraction( e:Event ):void
		{
			e.stopImmediatePropagation();
		}

		
	}
}
